package dictcn.com.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import dictcn.com.R;
import dictcn.com.R.id;
import dictcn.com.R.layout;
import dictcn.com.control.MyDatabaseHelper;
import dictcn.com.control.MytabOperate;
import dictcn.com.model.BookName;

public class DeleteBookName extends Activity{//删除或者重命名的activity

	private ListView lv_delteView=null;
	private ImageButton imgBack=null;
	private List<BookName> list=null;
	private List<Map<String,Object>> listItem=null;
	private MyAdapter myAdapter=null;
	private SQLiteOpenHelper helper=null;
	private MytabOperate mytab=null;
	private List<Integer> listIsChecked=null;
	private Button btn_delete=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.word_list_edit);
		listIsChecked=new ArrayList<Integer>();
		initView();
	}
	
	@SuppressWarnings("unchecked")
	public void initView(){	
		lv_delteView=(ListView)findViewById(R.id.lv_list_delete_word);	
		btn_delete=(Button)findViewById(R.id.btn_delete_word);
		imgBack=(ImageButton)findViewById(R.id.imgBtnDeleteBack);
		Bundle boundle=getIntent().getBundleExtra("boundle");
		list=new ArrayList<BookName>();
		list=(List<BookName>) boundle.getSerializable("wordList");
		listItem=new ArrayList<Map<String,Object>>();
		HashMap<String,Object> map;
		for(int i=0;i<list.size();i++){
			  map=new HashMap<String,Object>();
			  map.put("BookName", list.get(i).getB_name());
			  map.put("BookNameCount", "生词数： "+list.get(i).getCount());
			  listItem.add(map);
		 }	         
		myAdapter=new MyAdapter(this);    
	    lv_delteView.setAdapter(myAdapter);
	    
	    imgBack.setOnClickListener(new OnClickListener() {
	    	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(DeleteBookName.this, WordList.class);
				startActivity(intent);
				DeleteBookName.this.finish();
			}
		} );
	    
	    //删除按钮监听
	    btn_delete.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name=null;
				for(int i=0;i<listIsChecked.size();i++)
				{
					name=(String)listItem.get(listIsChecked.get(i)).get("BookName");
					
					helper=new MyDatabaseHelper(DeleteBookName.this);
				    helper.getWritableDatabase();
					mytab=new MytabOperate(
					    	helper.getWritableDatabase());
					mytab.deleteBookNameFromBookName(name);
				}
				Toast.makeText(DeleteBookName.this, "删除成功！", Toast.LENGTH_SHORT).show();
				btn_delete.setEnabled(false);
				listIsChecked.clear();
				setData();
			}
		});
	}
	
	//自定义listView显示，主要包含checkBox和按钮的监听
	public final class ViewHolder{
		
		public CheckBox checkBox_btn;
		public Button btn_rename=null;
		public TextView txt_name;
		public TextView txt_count;
	}
	
	
	public class MyAdapter extends BaseAdapter{

		private LayoutInflater mInflater;
		
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}
		
		public MyAdapter(Context context){
			mInflater=LayoutInflater.from(context);
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder=null;
			if(convertView==null){
				holder=new ViewHolder();
				convertView=mInflater.inflate(R.layout.word_list_edit_item, null);
				holder.checkBox_btn=(CheckBox)convertView.findViewById(R.id.checkBox_word_edit);
				holder.txt_name=(TextView)convertView.findViewById(R.id.txtWord_editName);
				holder.txt_count=(TextView)convertView.findViewById(R.id.txtWord_editCount);
				holder.btn_rename=(Button)convertView.findViewById(R.id.btn_rename_word);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder)convertView.getTag();
			}
			
			holder.txt_name.setText((String)listItem.get(position).get("BookName"));
			holder.txt_count.setText((String)listItem.get(position).get("BookNameCount"));
			
			
			//重命名按钮监听
			holder.btn_rename.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					//自定义对话框的显示
					LayoutInflater inflater=LayoutInflater.from(DeleteBookName.this);
					View view=inflater.inflate(R.layout.word_list_dlg, null);
					final EditText edit=(EditText)view.findViewById(R.id.edit_wordlist_content);
					Dialog dlg=new AlertDialog.Builder(DeleteBookName.this)
									.setView(view)
									.setPositiveButton("确定", new DialogInterface.OnClickListener() {
										
										public void onClick(DialogInterface dialog, int which) {
											// TODO Auto-generated method stub
											String content=edit.getText().toString().trim();
											if(content.equals("")){
												Toast.makeText(DeleteBookName.this, "生词本名字不能为空", Toast.LENGTH_LONG).show();
											}else{
												
												//以下为重命名按钮的数据库操作实现
												
												helper=new MyDatabaseHelper(DeleteBookName.this);
											    helper.getWritableDatabase();
												mytab=new MytabOperate(
												    	helper.getWritableDatabase());
												boolean flag=mytab.SelctBookName(content);
												if(!flag){		
													helper=new MyDatabaseHelper(DeleteBookName.this);
												    helper.getWritableDatabase();
													mytab=new MytabOperate(
													    	helper.getWritableDatabase());
													mytab.UpdateBookNameFromBookName((String)listItem.get(position).get("BookName"),content);
													Toast.makeText(DeleteBookName.this, "生词本名称修改成功", Toast.LENGTH_SHORT).show();
													setData();
												}else{
													Toast.makeText(DeleteBookName.this, "生词本名称已存在", Toast.LENGTH_SHORT).show();
													edit.setText("");
												}
											}
										}
									})
									.setNegativeButton("取消", null)
									.setTitle("修改后的文件名")
									.create();
					dlg.show();
				}
			});
			
			//删除按钮监听
			holder.checkBox_btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked){
						listIsChecked.add(position);
						btn_delete.setEnabled(true);
					}else{
						int pos=listIsChecked.indexOf(position);
						listIsChecked.remove(pos);
						if(listIsChecked.isEmpty())
							btn_delete.setEnabled(false);
					}						
				}
			});
			return convertView;
		}	
	}
	
	//listView中内容的设置的控件的关联
	public void setData(){
		helper=new MyDatabaseHelper(DeleteBookName.this);
	    helper.getWritableDatabase();
		mytab=new MytabOperate(
		    	helper.getWritableDatabase());
		list=new ArrayList<BookName>();
		list=mytab.SelctAllBookName();
		listItem=new ArrayList<Map<String,Object>>();
		HashMap<String,Object> map;
		for(int i=0;i<list.size();i++){
			  map=new HashMap<String,Object>();
			  map.put("BookName", list.get(i).getB_name());
			  map.put("BookNameCount", "生词数： "+list.get(i).getCount());
			  listItem.add(map);
		 }	         
		myAdapter=new MyAdapter(this);    
	   lv_delteView.setAdapter(myAdapter);
	}
	
	//返回键的监听操作
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			DeleteBookName.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
